#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <iostream>
#include <vector>
#include <string>

using namespace std;


#define SERVER_PORT 2345
#define SERVER_IP "127.0.0.1"

#define MY_PORT 8000

int useage();
int list_number();
int pass_file();


struct nodeinfo
{
    char group[256];
    char passwd[64];
};

struct nodelist
{
    char nodeip[32];
    int  nodeport;
};

vector <string> number_list;

int main(int argc, char *argv[] )
{
   
   if(argc < 2)
   {
      useage();
      exit(0);
   }

//   int i=0;
//   for( i; i < argc; i++)
//   {
//       printf("argc[%d] =  %s\n",i,argv[i]);
//   }

    switch(atoi(argv[1]))
     {
         case 1:  
             //printf("list all number\n");
             list_number();
             break;
 
         case 2:   
             printf("pass a file a number\n");
             pass_file();
             break;
         default:  useage();
 
     }



    return 0;
}



int useage()
{
    printf("1. list all numeber\n");
    printf("2. pass a file a number\n");
    printf("\n");
}


int list_number()
{
    int s,len;
    struct sockaddr_in my_addr,peer_addr;
    struct nodeinfo node1;

    int addr_len = sizeof(struct sockaddr_in);
    char buffer[256];

    if((s = socket(AF_INET,SOCK_DGRAM,0)) < 0)
    {
        perror("socket");
        exit(1);
    }

    memset(&my_addr,0,sizeof(my_addr));
    memset(&peer_addr,0,sizeof(peer_addr));

    my_addr.sin_family      = AF_INET;
    my_addr.sin_port        = htons(MY_PORT);
    my_addr.sin_addr.s_addr = htonl(INADDR_ANY);

    peer_addr.sin_family       = AF_INET;
    peer_addr.sin_port         = htons(SERVER_PORT);
    peer_addr.sin_addr.s_addr  = inet_addr(SERVER_IP);
    

    if(bind(s,(struct sockaddr *) &my_addr,sizeof(my_addr))<0 )
    {
        perror("connect");
        exit(1);
    }

//    while(1)
//    {
//        bzero(buffer,sizeof(buffer));
//        len = read (STDIN_FILENO,buffer,sizeof(buffer));
//        sendto(s,buffer,len,0,&my_addr,addr_len);
//        printf("addr %d\n",my_addr.sin_port);
//        len = recvfrom(s,buffer,sizeof(buffer),0,&peer_addr,&addr_len);
//        printf("receive from %s  %d\n",inet_ntoa(peer_addr.sin_addr),ntohs(peer_addr.sin_port));
//    }

    strcpy(node1.group,"lanve");
    strcpy(node1.passwd,"123456");

    sendto(s,&node1,sizeof(node1),0,(struct sockaddr *)&peer_addr,addr_len);
    while(1)
    {
        memset(buffer,0,sizeof(buffer));
        len = recvfrom(s,buffer,sizeof(buffer),0,(struct sockaddr *)&peer_addr,(socklen_t *) &addr_len);
        //printf("receive from %s  %d, content : %s\n",inet_ntoa(peer_addr.sin_addr),ntohs(peer_addr.sin_port),buffer);
        
        if (strcmp(buffer,"end") != 0)
        {
            number_list.push_back( buffer );
        }
        else
        {
            break;
        }
    }

    int i = 1;
    vector<string>::iterator it;
    for ( it=number_list.begin() ; it < number_list.end(); it++ )
    {
        cout << i++ <<  " : " << *it;
        cout << endl;
    }

}

int pass_file()
{
    int i;
    char cmd[256];

    printf ("Checking if processor is available...");
    if (system(NULL)) puts ("Ok");
      else exit (1);

    memset(cmd,0,sizeof(cmd));
    sprintf (cmd, "./udt/appclient %s %d", "127.0.0.1",9000);
    printf ("Executing command : %s ...\n",cmd);
    i=system ( cmd );
    printf ("The value returned was: %d.\n",i);
    return 0;
}
